package net.tv90.bfmccore.game.view;

public class MapCoordConverter {
    /**
     * World coordinate of the first reference point (usually top‑left corner).
     */
    protected int mapRefX1;
    /**
     * World coordinate of the first reference point (usually top‑left corner).
     */
    protected int mapRefZ1;
    /**
     * World coordinate of the second reference point (usually bottom‑right corner).
     */
    protected int mapRefX2;
    /**
     * World coordinate of the second reference point (usually bottom‑right corner).
     */
    protected int mapRefZ2;
    /**
     * Width of the map panel on the UI.  This is the pixel width of the
     * minimap image itself, not including surrounding chrome.
     */
    protected int uiMapWidth;
    /**
     * Height of the map panel on the UI.  This is the pixel height of the
     * minimap image itself, not including surrounding chrome.
     */
    protected int uiMapHeight;
    /**
     * Total width of the UI container in which the minimap sits (e.g. the
     * full GUI width).  This value is used to centre the minimap within
     * the available space.
     */
    protected int relativeWidth;
    /**
     * Total height of the UI container in which the minimap sits (e.g. the
     * full GUI height).  This value is used to centre the minimap within
     * the available space.
     */
    protected int relativeHeight;

    /**
     * Sets up the mapping between world space and UI space.
     *
     * <p>
     * The mapping is defined by two points in the world which correspond
     * to the corners of the minimap.  The first point should represent
     * the top‑left of the playable area, and the second the bottom‑right.
     * The method also takes the dimensions of the minimap image and the
     * overall UI container.  Once configured, calls to
     * {@link #worldToScreen(int, int)} will return the screen position
     * of any world X/Z coordinate relative to this mapping.
     *
     * @param x1 world X coordinate of the first reference point
     * @param z1 world Z coordinate of the first reference point
     * @param x2 world X coordinate of the second reference point
     * @param z2 world Z coordinate of the second reference point
     * @param uiWidth width of the map panel on the UI in pixels
     * @param uiHeight height of the map panel on the UI in pixels
     * @param relWidth total width of the UI container in pixels
     * @param relHeight total height of the UI container in pixels
     */
    public void setMapCoordinateReference(int x1, int z1, int x2, int z2,
                                                 int uiWidth, int uiHeight,
                                                 int relWidth, int relHeight) {
        mapRefX1 = x1;
        mapRefZ1 = z1;
        mapRefX2 = x2;
        mapRefZ2 = z2;
        uiMapWidth = uiWidth;
        uiMapHeight = uiHeight;
        relativeWidth = relWidth;
        relativeHeight = relHeight;
    }

    public int[] worldToScreen(int worldX, int worldZ) {
        float percentX;
        float percentZ;
        if (mapRefX1 < mapRefX2 && mapRefZ1 < mapRefZ2) {
            percentX = (float) (worldX - mapRefX1) / (mapRefX2 - mapRefX1);
            percentZ = (float) (worldZ - mapRefZ1) / (mapRefZ2 - mapRefZ1);
        } else if (mapRefX1 > mapRefX2 && mapRefZ1 > mapRefZ2) {
            percentX = (float) (mapRefX1 - worldX) / (mapRefX1 - mapRefX2);
            percentZ = (float) (mapRefZ1 - worldZ) / (mapRefZ1 - mapRefZ2);
        } else if (mapRefX1 > mapRefX2 && mapRefZ1 < mapRefZ2) {
            percentX = (float) (worldZ - mapRefZ1) / (mapRefZ2 - mapRefZ1);
            percentZ = (float) (mapRefX1 - worldX) / (mapRefX1 - mapRefX2);
        } else {
            percentX = (float) (mapRefZ1 - worldZ) / (mapRefZ1 - mapRefZ2);
            percentZ = (float) (worldX - mapRefX1) / (mapRefX2 - mapRefX1);
        }
        percentX = Math.max(0, Math.min(1, percentX));
        percentZ = Math.max(0, Math.min(1, percentZ));
        int screenX = Math.round(percentX * uiMapWidth);
        int screenY = Math.round(percentZ * uiMapHeight);
        screenX += (relativeWidth - uiMapWidth) / 2;
        screenY += (relativeHeight - uiMapHeight) / 2;
        return new int[]{screenX, screenY};
    }
}
